package com.hdax.dm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.dto.ScheduleDto;
import com.hdax.dm.entity.scheduler.DmSchedulerSeat;
import com.hdax.dm.mappers.SeatMapper;
import com.hdax.dm.mapstrucs.ScheduleDtoMapper;
import com.hdax.dm.service.SchedulerService;
import com.hdax.dm.service.SeatService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SeatSImpl extends ServiceImpl<SeatMapper, DmSchedulerSeat> implements SeatService {

    @Autowired
    private ScheduleDtoMapper scheduleDtoMapper;

    @Autowired
    private SchedulerService schedulerService;

    @Override
    public List<ScheduleDto.SeatInfoDto> seatInfo(Long scheduleId) {
        QueryWrapper<DmSchedulerSeat> wrapper=new QueryWrapper<>();
        wrapper.eq("scheduleId",scheduleId);
        wrapper.isNull("orderNo");
        wrapper.isNull("userId");
        wrapper.eq("status",1);
        List<DmSchedulerSeat> seats= list(wrapper);
        List<ScheduleDto.SeatInfoDto> scheduleDto= scheduleDtoMapper.seatToTargets(list(wrapper));
        /*for (int i = 0; i < seats.size(); i++) {
            if (seats.get(i).getAreaLevel()==1){
                scheduleDto.get(i).setAreaLevel("A");
            }else  if (seats.get(i).getAreaLevel()==2){
                scheduleDto.get(i).setAreaLevel("B");
            }else if (seats.get(i).getAreaLevel()==3){
                scheduleDto.get(i).setAreaLevel("C");
            }
        }*/
        return scheduleDto;
    }

    @Override
    public CommonResponse<Map<String,List<String>>> getSeatList(Long cinemaId,Long scheduleId) {
        Map<String, List<String>> map=new HashMap<>();

        QueryWrapper<DmSchedulerSeat> wrapper=new QueryWrapper<>();
        wrapper.eq("scheduleId",scheduleId);
        List<DmSchedulerSeat> seatList= list(wrapper);

        Map<Long,List<DmSchedulerSeat>> mapX=new HashMap<>();
        //把seats集合转成map结构       //collect：重构        以x进行分组
        mapX = seatList.stream().collect(Collectors.groupingBy(DmSchedulerSeat::getX));

        List<String> seatArray=new ArrayList<>();
        //获取mapX中的每一个值
        mapX.values().forEach(seats->{
            StringBuffer buffer=new StringBuffer();
            seats.forEach(seat->{
                if(seat.getAreaLevel()==0L){
                    buffer.append("_");
                }else if (seat.getAreaLevel()==1L){
                    buffer.append("a");
                }else if (seat.getAreaLevel()==2L) {
                    buffer.append("b");
                }else if (seat.getAreaLevel()==3L) {
                    buffer.append("c");
                }
            });
            seatArray.add(buffer.toString());
        });

        map.put("seatArray",seatArray);
        return ResponseUtil.returnSuccess(map);
    }

    @Override
    public DmSchedulerSeat seats(Long scheduleId,Long x, Long y) {
        QueryWrapper<DmSchedulerSeat> wrapper=new QueryWrapper<>();
        wrapper.eq("X",x);
        wrapper.eq("Y",y);
        wrapper.eq("scheduleId",scheduleId);
        wrapper.isNull("orderNo");
        wrapper.isNull("userId");
        wrapper.eq("status",1);
        return getOne(wrapper);
    }

    @Override
    public Integer seatUpdate(List<DmSchedulerSeat> seat) {
        Integer update=null;
        for (DmSchedulerSeat s : seat) {
            QueryWrapper<DmSchedulerSeat> wrapper=new QueryWrapper<>();
            wrapper.eq("x",s.getX());
            wrapper.eq("y",s.getY());
            wrapper.eq("scheduleId",s.getScheduleId());
            update= baseMapper.update(s,wrapper);
        }
        return update;
    }

    @Override
    public Boolean ishaveSeatUpdate(Long status,String orderNo) {
        DmSchedulerSeat seat=new DmSchedulerSeat();
        QueryWrapper<DmSchedulerSeat> wrapper = new QueryWrapper();
        wrapper.eq("orderNo",orderNo);
        seat.setStatus(status);
        if(status==1L){
            seat.setOrderNo(null);
            seat.setUserId(null);
        }
        return update(seat, wrapper);
    }
}
